﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace theRift
{
    public class CameraManager : Microsoft.Xna.Framework.GameComponent
    {
        protected int activeCameraIndex;
        protected Camera activeCamera;
        protected SortedList<string, Camera> cameras;

        public int ActiveCameraIndex
        {
            get { return activeCameraIndex; }
        }

        public Camera ActiveCamera
        {
            get { return activeCamera; }
        }

        public Camera this[int index]
        {
            get { return cameras.Values[index]; }
        }

        public Camera this[string id]
        {
            get { return cameras[id]; }
        }

        public int Count
        {
            get { return cameras.Count; }
        }

        public CameraManager(Game game)
            : base(game)
        {
            cameras = new SortedList<string, Camera>(4);
            activeCameraIndex = -1;
        }

        public void SetActiveCameraIndex(int cameraIndex)
        {
            activeCameraIndex = cameraIndex;
            activeCamera = cameras[cameras.Keys[cameraIndex]];
        }

        public void SetActiveCamera(string id)
        {
            activeCameraIndex = cameras.IndexOfKey(id);
            activeCamera = cameras[id];
        }

        public void Clear()
        {
            cameras.Clear();
            activeCamera = null;
            activeCameraIndex = -1;
        }

        public void Add(string id, Camera camera)
        {
            cameras.Add(id, camera);

            if (activeCamera == null)
            {
                activeCamera = camera;
                activeCameraIndex = -1;
            }
        }

        public void Remove(string id)
        {
            cameras.Remove(id);
        }

        public override void Update(GameTime gameTime)
        {
            activeCamera.Update(gameTime);
            
            base.Update(gameTime);
        }
    }
}